package com.aube.interact.controller;

import android.content.Context;
import android.view.ViewGroup;

import com.aube.interact.model.GoodsItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huyaonan on 16/3/23.
 */
public abstract class AbstractController<T> {

    public static final String API_ROUTER = "http://192.168.1.254/openapi2/router/rest2";//"http://openapi.aube-tv.com/router/rest2"

    public final String TAG = getClass().getSimpleName();

    protected IControllerCallback iControllerCallback;

    protected ViewGroup mCardRoot;

    protected Context context;
    protected String mVideoId;

    public AbstractController(Context context, ViewGroup mRoot, String mVideoId, IControllerCallback callback) {
        this.context = context;
        this.mVideoId = mVideoId;
        this.mCardRoot = mRoot;
        this.iControllerCallback = callback;
    }

    protected List<T> mCardsList = new ArrayList<>();

    /**
     * 组装卡片顺序列表,根据当前播放位置来装入卡片,如果是刚开始,则selectedIndex为0
     */
    public abstract boolean setupCards(long selectedIndex, boolean freshData, boolean autoBegin);

    protected T mCurrentData = null;

    /**
     * 根据播放位置加载卡片
     * @param current
     */
    public abstract boolean locateCardByTime(long current);

    public void clearCardsList() {
        synchronized (mCardsList) {
            mCardsList.clear();
        }
    }

    public abstract boolean manageCard(T data);

    public abstract void appendToScreen(T data);

    public void removeFromScreen() {
        mCardRoot.removeAllViews();
    }

    public void end() {
        mCurrentData = null;
        removeFromScreen();
        clearCardsList();
    }

    public void onDestroy() {
        iControllerCallback = null;
        clearCardsList();
    }

    public boolean onBack() {
        return false;
    }

    public interface IControllerCallback {
        public void onGoodsAdd(List<GoodsItem> data);
        public void cancelGoods(String id);
        public long getCurrentProgress();
        public int[] getCartLocation();
        public void hideFavorCart();
    }

}
